#include <cmath>
#include "gui/objects/segments/GUIRoadSegment.h"
#include "util/MathUtils.h"

namespace gui {

GUIRoadSegment::GUIRoadSegment(GUIObjectDrawer* drawer, RoadSegment* roadSegment, std::string filename, RGBColor transparentColor, bool keepSize) :
	GUIObject(drawer, filename, transparentColor) {

	Intersection* origin = roadSegment->getOrigin();
	Intersection* destination = roadSegment->getDestination();
	this->setCenterInMeters((origin->getX() + destination->getX()) / 2.0, (origin->getY() + destination->getY()) / 2.0);
	if (!keepSize) {
		this->setSizeInMeters(-1.0, this->getRoadSegmentLength(*origin, *destination));
	}
	this->setRotation(roadSegment->getAngle());

}

GUIRoadSegment::GUIRoadSegment(GUIObjectDrawer* drawer, MapPoint origin, MapPoint destination, std::string filename, RGBColor transparentColor, bool keepSize) :
	GUIObject(drawer, filename, transparentColor) {
	this->setDimensionsAndLocation(origin, destination, keepSize);
}

GUIRoadSegment::~GUIRoadSegment() { }

double GUIRoadSegment::getRoadSegmentLength(MapPoint o, MapPoint d) {
	double x = d.getX() - o.getX();
	double y = d.getY() - o.getY();
	return sqrt(x * x + y * y);
}

void GUIRoadSegment::setDimensionsAndLocation(MapPoint origin, MapPoint destination, bool keepSize) {
       this->setCenterInMeters((origin.getX() + destination.getX()) / 2.0, (origin.getY() + destination.getY()) / 2.0);

       double height = keepSize ? -1.0 : this->getRoadSegmentLength(origin, destination);
       this->setSizeInMeters(-1.0, height);
       double angle = atan2((destination.getY() - origin.getY()) , (destination.getX() - origin.getX())) + PI / 2;
       this->setRotation(angle);
}

}
